package com.sctvcloud.qingchuan.ui.activities;

import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ProgressBar;

import com.canyinghao.canrefresh.CanRefreshLayout;
import com.canyinghao.canrefresh.other.CanRecyclerViewHeaderFooter;
import com.ruihang.generalibrary.ui.util.OnItemInternalClick;
import com.ruihang.generalibrary.utils.ListUtils;
import com.sctvcloud.qingchuan.R;
import com.sctvcloud.qingchuan.application.Constances;
import com.sctvcloud.qingchuan.base.BaseAccountActivity;
import com.sctvcloud.qingchuan.beans.AccountCommentBean;
import com.sctvcloud.qingchuan.beans.AccountCommentsVo;
import com.sctvcloud.qingchuan.http.AbsNetCallBack;
import com.sctvcloud.qingchuan.http.NetUtils;
import com.sctvcloud.qingchuan.ui.adapter.AccountCommentAdapter;
import com.sctvcloud.qingchuan.ui.dialog.CommentDiaFragment;
import com.sctvcloud.qingchuan.ui.utils.DataUtils;
import com.sctvcloud.qingchuan.ui.utils.TitleUtils;
import com.sctvcloud.qingchuan.utils.UserManager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.RequestBody;

/**
 * 融媒号 评论列表
 */
public class AccountCommentActivity extends BaseAccountActivity implements CanRefreshLayout.OnRefreshListener
        , CanRecyclerViewHeaderFooter.OnLoadMoreListener , BaseAccountActivity.OnAddCommentListener {
    @BindView(R.id.comment_refresh)
    protected CanRefreshLayout refreshLayout;
    @BindView(R.id.can_content_view)
    protected RecyclerView comment_list;
    @BindView(R.id.comment_footer)
    protected CanRecyclerViewHeaderFooter footer;
    @BindView(R.id.comment_foot_pb)
    protected ProgressBar pb;
    @BindView(R.id.comment_foot_loadmore)
    protected View allLoaded;
    private LinearLayoutManager manager;
    private AccountCommentAdapter adapter;
    private List<AccountCommentBean> commentList = new ArrayList<>();
    private CommentDiaFragment commentDiaFragment;
    public static final String COMMENT_TAG = "dia_comment_fragment";
    private String id;
    private int indexNumber = 0;
    private int pageNo = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        new TitleUtils(this);
        initView();
    }

    @Override
    protected void initInject() {
        setContentView(R.layout.activity_comment);
        ButterKnife.bind(this);
    }

    private void initView() {
        refreshLayout.setOnRefreshListener(this);
        manager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        comment_list.setItemAnimator(new DefaultItemAnimator());
        comment_list.setLayoutManager(manager);

        id = getIntent().getStringExtra(EX_ID);

        footer.attachTo(comment_list, false);
        footer.setLoadMoreListener(this);
        footer.setVisibility(View.GONE);

        refreshLayout.autoRefresh();
    }

    @OnClick(value = {R.id.comment_write_comment})
    public void commentClick(View view) {

    }

    @Override
    public void onLoadMore() {
        if (refreshLayout.isRefreshing()) {
            footer.loadMoreComplete();
        } else {
            indexNumber += 20;
            getCommentData();
        }
    }

    @Override
    public void onChildAttached(View v) {
    }

    @Override
    public void onRefresh() {
        pageNo = 1;
        getCommentData();
    }

    private void getCommentData() {
        Map<String, String> formMaildata = new HashMap<>();
        formMaildata.put("relaId", id);
        formMaildata.put("siteId", Constances.ACCOUNT_SITE_ID);
        formMaildata.put("page", "" + pageNo);
        formMaildata.put("pageSize", "10");


        Map<String, RequestBody> bodyMap = DataUtils.generateRequestBody(formMaildata);
        NetUtils.getNetAdapter().toGetCommentString(getOwnerName(), bodyMap, new AbsNetCallBack<String>(String.class) {
            @Override
            public void onSucStr(String news) {
                if (!TextUtils.isEmpty(news)) {
                    try {
                        AccountCommentsVo commentsVo = gson.fromJson(news, AccountCommentsVo.class);
                        if (null != commentsVo && ListUtils.isListValued(commentsVo.getCommentList())) {
                            commentList.addAll(commentsVo.getCommentList());
                        }
                    } catch (Exception e) {
                        e.printStackTrace();

                    }
                } else {
                    onError(null, "数据异常");
                }
            }

            @Override
            public void onError(Throwable ex, String errorMsg) {
//                    toast(errorMsg);
            }

            @Override
            public void onEnd() {
                if(ListUtils.isListValued(commentList)){
                    initInfo(commentList);
                }
            }
        });


    }

    @Override
    protected void onCommentSendSuc(int mode) {
        super.onCommentSendSuc(mode);
        onRefresh();
    }

    private synchronized void onNetFinish() {
        if (refreshLayout.isRefreshing()) {
            refreshLayout.refreshComplete();
        } else {
            footer.loadMoreComplete();
        }


    }

    private void initInfo(List<AccountCommentBean> datas) {
        List<AccountCommentBean> data = dataHandle(datas);
        if (adapter == null) {
            adapter = new AccountCommentAdapter(this, data);
            adapter.setItemInternalClick(commentItemClick);
            comment_list.setAdapter(adapter);
        } else {
            if (pageNo == 1) {
                adapter.setData(data);
            } else {
                adapter.addDatas(data);
            }
        }
        if (adapter.getItemCount() == 0) {
            footer.setVisibility(View.GONE);
        } else {
            footer.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 处理评论数据
     *
     * @param datas
     * @return
     */
    private List<AccountCommentBean> dataHandle(List<AccountCommentBean> datas) {
        List<AccountCommentBean> comments = new ArrayList<>();
        if (null != datas) {

            for (int i = 0; i < datas.size(); i++) {
                AccountCommentBean comment =datas.get(i);
                comments.add(comment);
                if (ListUtils.isListValued(comment.getCommentReplayLis())) {
                    for (int k = 0; k < comment.getCommentReplayLis().size(); k++) {
                        comments.add(comment.getCommentReplayLis().get(k));
                    }
                }
            }
        }
        return comments;

    }

    @OnClick({R.id.comment_layout_write_comment})
    public void newesClick(View view) {

        switch (view.getId()) {
            case R.id.comment_layout_write_comment:
                AccountCommentBean comment = new AccountCommentBean();
                comment.setRelaId("" + id);
                comment.setAddUser(UserManager.getInstance().getUser().getUserId());
                comment.setType("1");
                int flag = 1;
                showCommentDialog(comment, flag, this);  //0代表新闻评论
                break;

        }

    }

    @Override
    public void OnAddComment(boolean isSuccess) {

    }

    private OnItemInternalClick commentItemClick = new OnItemInternalClick() {
        @Override
        public void onItemInternalClick(View itemView, View view, int position) {
//            FComment comment = null;
//            if (view == null) {
//                comment = (FComment) itemView.getTag(R.id.adapter_item_data_key);
//            } else {
//                comment = (FComment) itemView.getTag(R.id.adapter_item_data_key);
//            }
            AccountCommentBean comment = adapter.getItem(position);
//            if (!TextUtils.isEmpty(comment.getCommentId())) {
//                if (TextUtils.isEmpty(baseConstUrl)) {
//                    showCommentDialog(id, comment.getCommentId(), commentType);
//                } else {
//                    showCommentDialog(id, true, comment.getCommentId(), commentType);
//                }

//            } else {
//                toast("评论id为空");
//            }
            //showCommentDialog(id, comment.getCommentId(), commentType);
        }
    };
}
